﻿using H2.NET.Data.Common;
using H2.NET.Web.Pages.SMD.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace H2.NET.Web.Pages.SMD.Helper
{
    public static class DataHelper<T>
    {
        public static List<t_StoreDataChangeLog> GetChangeLogs(T oldObj, T newObj, Field[] fields, string keyName, string userId, string assessmentId, string category,string ShortName="")
        {

            Type type = oldObj.GetType();
            PropertyInfo propertyInfo = type.GetProperty(keyName);
            var dataID = propertyInfo.GetValue(oldObj, null);
            List<t_StoreDataChangeLog> changelogs = new List<t_StoreDataChangeLog>();
            foreach (Field filed in fields)
            {
                propertyInfo = type.GetProperty(filed.Name);
                var value = propertyInfo.GetValue(oldObj, null);
                if (filed.Value == null && value == null) continue;
                if (filed.Value != null && value == null) if (filed.Value.Equals(value)) continue;
                if (filed.Value == null && value != null) if (value.Equals(filed.Value)) continue;
                if (filed.Value != null && value != null) if (value.Equals(filed.Value)) continue;
                if (filed.Value is DateTime || value is DateTime)
                {
                    if (filed.Value != null && value != null)
                    {
                        var x = Convert.ToDateTime(filed.Value.ToString());
                        var y = Convert.ToDateTime(value.ToString());
                        if (x.Year == y.Year && x.Month == y.Month && x.Day == y.Day)
                        {
                            continue;
                        }
                    }
                }
                if (filed.Value is String || value is String)
                {
                    if (filed.Value == null) filed.Value = "";
                    if (value == null) value = "";
                    if (filed.Value == value || filed.Value.Equals(value))
                    {
                        continue;
                    }
                }
                if (filed.Value is Int32 || value is Int32)
                {
                    if (filed.Value == null) filed.Value = 0;
                    if (value == null) value = 0;
                    if (filed.Value == value || filed.Value.Equals(value))
                    {
                        continue;
                    }
                }

                t_StoreDataChangeLog changelog = new t_StoreDataChangeLog();
                changelog.Category = category;
                changelog.DataID = dataID.ToString();
                changelog.FieldName = filed.Name;
                changelog.AfterValue = filed.Value == null ? "" : filed.Value.ToString();
                changelog.BeforeValue = value == null ? "" : value.ToString();
                changelog.ChangeTime = DateTime.Now;
                changelog.UserID = userId;
                changelog.AssessmentID = assessmentId;
                changelogs.Add(changelog);
            }
            if (!string.IsNullOrWhiteSpace(ShortName))
            {
                t_StoreDataChangeLog changelog = new t_StoreDataChangeLog();
                changelog.Category = category;
                changelog.DataID = dataID.ToString();
                changelog.FieldName = "ShortName";
                changelog.AfterValue = ShortName;
                changelog.BeforeValue = null;
                changelog.ChangeTime = DateTime.Now;
                changelog.UserID = userId;
                changelog.AssessmentID = assessmentId;
                changelog.ChangeReason = ShortName;
                changelogs.Add(changelog);
            }

            return changelogs;
        }

        public static T Reload(T obj, List<t_StoreDataChangeLog> changelogs)
        {
            Type type = obj.GetType();
            foreach (var changelog in changelogs)
            {
                PropertyInfo propertyInfo = type.GetProperty(changelog.FieldName);
                propertyInfo.SetValue(obj, EntityBase.ChangeType(changelog.AfterValue, propertyInfo.PropertyType), null);
            }

            return obj;
        }
        public static T Reload(T obj, List<t_smd_StoreDataChangeLog> changelogs)
        {
            Type type = obj.GetType();
            foreach (var changelog in changelogs)
            {
                PropertyInfo propertyInfo = type.GetProperty(changelog.FieldName);
                propertyInfo.SetValue(obj, EntityBase.ChangeType(changelog.AfterValue, propertyInfo.PropertyType), null);
            }

            return obj;
        }
    }
}